<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="黑匣子唯一标识:">
              <a-input style="width: 200px" v-model:value.trim="where.vehicleImei" placeholder="请输入黑匣子唯一标识" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="vehicleId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >

          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a @click="openBindDeviceDialog(record)">绑定黑匣子</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
                <a-divider type="vertical" />
                <a @click="openShowWorkruleEdit(record)">作业规则</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <vehicle-manager-vehicle-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />

    <vehicle-manager-vehicle-binddevice v-if="showBindDevice" v-model:visible="showBindDevice" :data="current" @done="reload" />

    <VehicleManagerVehicleWorkruleEdit v-if="showWorkruleEdit" v-model:visible="showWorkruleEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { VehicleApi } from '@/api/property/vechile/VehicleApi';
import { message } from 'ant-design-vue';
import VehicleManagerVehicleEdit from './vehicle-edit.vue'
import VehicleManagerVehicleBinddevice from './binddevice.vue'
import VehicleManagerVehicleWorkruleEdit from './vehicle-workrule-edit.vue'

export default {
  name: 'VehicleManagerVehicle',
  components: {
    PlusOutlined,
    VehicleManagerVehicleEdit,
    VehicleManagerVehicleBinddevice,
    VehicleManagerVehicleWorkruleEdit
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '车辆id',
          dataIndex: 'vehicleId'
        },
        {
          title: '黑匣子设备',
          dataIndex: 'deviceRecord',
          customRender: function(row) {
            console.log(row)
            if (row.record.deviceRecord != null && row.record.deviceRecord.recordId > 0) {
              return  row.record.deviceRecord.clientNum + "(设备id:" + row.record.deviceRecord.recordId + ")";
            } else {
              return "未关联黑匣子";
            }
          }
        },
        {
          title: '车辆名字',
          dataIndex: 'vehicleName'
        },
        {
          title: '车牌号',
          dataIndex: 'license'
        },
        {
          title: '载客数',
          dataIndex: 'passenger'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showBindDevice : false,
      showWorkruleEdit : false,
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reset() {
      this.where.vehicleImei = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    async remove(row) {
      const result = await VehicleApi.delete({ vehicleId: row.vehicleId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    openShowWorkruleEdit(row) {
      this.current = row;
      this.showWorkruleEdit = true;
    },

    openBindDeviceDialog(row) {
      this.current = row;
      this.showBindDevice = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return VehicleApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
